load(
    "@gz//bazel/skylark:build_defs.bzl",
    "GZ_FEATURES",
    "GZ_ROOT",
    "GZ_VISIBILITY",
    "gz_configure_header",
    "gz_export_header",
    "gz_include_header",
)
load(
    "@gz//bazel/lint:lint.bzl",
    "add_lint_tests",
)
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [GZ_ROOT + "common:license"],
    default_visibility = GZ_VISIBILITY,
    features = GZ_FEATURES,
)

license(
    name = "license",
    package_name = "gz-common",
)

licenses(["notice"])

exports_files(["LICENSE"])

gz_configure_header(
    name = "config",
    src = "include/gz/common/config.hh.in",
    cmakelists = ["CMakeLists.txt"],
    package = "common",
)

gz_export_header(
    name = "include/gz/common/Export.hh",
    export_base = "GZ_COMMON",
    lib_name = "gz-common",
    visibility = ["//visibility:private"],
)

public_headers_no_gen = glob([
    "include/gz/common/*.hh",
    "include/gz/common/detail/*.hh",
    "include/gz/common/graph/*.hh",
])

private_headers = glob(["src/*.hh"])

sources = glob(
    ["src/*.cc"],
    exclude = [
        "src/Plugin.cc",
        "src/PluginLoader.cc",
        "src/*_TEST.cc",
    ],
)

gz_include_header(
    name = "commonhh_genrule",
    out = "include/gz/common.hh",
    hdrs = public_headers_no_gen + [
        "include/gz/common/config.hh",
        "include/gz/common/Export.hh",
    ],
)

public_headers = public_headers_no_gen + [
    "include/gz/common/config.hh",
    "include/gz/common/Export.hh",
    "include/gz/common.hh",
]

cc_library(
    name = "common",
    srcs = sources + private_headers,
    hdrs = public_headers,
    copts = [
        "-fexceptions",
        "-Wno-unused-value",
    ],
    includes = ["include"],
    deps = [
        GZ_ROOT + "utils",
        GZ_ROOT + "math",
        "@uuid",
    ],
)

test_sources = glob(
    include = ["src/*_TEST.cc"],
    exclude = [
        "src/PluginUtils_TEST.cc",
        "src/PluginLoader_TEST.cc",
    ],
)

[cc_test(
    name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
    srcs = [src],
    copts = ["-fexceptions"],
    deps = [
        ":common",
        GZ_ROOT + "common/testing",
        "@gtest",
        "@gtest//:gtest_main",
    ],
) for src in test_sources]

add_lint_tests()
